'use strict';

define(['common/service/menuService'], function () {
    return function (dependencies) {
        var definition = {
            resolver: ['$q', '$rootScope', '$location', 'MenuService', function ($q, $rootScope, $location, menuService) {
                //权限判断
                var menus = menuService.getMenus();
                var menuUrlMap = menuService.getMenuUrlMap();
                var path = $location.path();
                var originPath = menuService.getOriginMenuUrl(path);
                if (path != '/login') {
                    if (menus.length == 0) {
                        $location.path('/login');
                        return false;
                    }
                    if (menuUrlMap[originPath] == null) {
                        $location.path(menuService.getDefaultMenuUrl());
                        return false;
                    }
                }

                var deferred = $q.defer();
                require(dependencies, function () {
                    $rootScope.$apply(function () {
                        deferred.resolve();
                    });
                });
                return deferred.promise;
            }]
        };
        return definition;
    }
});